﻿package com.badibadi.logic;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;










import net.tsz.afinal.FinalBitmap;

import com.badibadi.mytools.BitmapHelp;
import com.badibadi.mytools.ImageTools;
import com.badibadi.uniclubber.R;
//import com.lidroid.xutils.BitmapUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ImgFileListAdapter extends BaseAdapter{

	Context context;
//	BitmapUtils bitmapUtils;
	String filecount="filecount";
	String filename="filename";
	String imgpath="imgpath";
	List<HashMap<String, String>> listdata;
	Util util;
//	Bitmap[] bitmaps;
	private int index=-1;
	List<View> holderlist;
	
	public ImgFileListAdapter(Context context,List<HashMap<String, String>> listdata) {
		this.context=context;
		this.listdata=listdata;
//		bitmaps=new Bitmap[listdata.size()];
		util=new Util(context);
		holderlist=new ArrayList<View>();
//		bitmapUtils = BitmapHelp.getBitmapUtils(context);
	}
	
	@Override
	public int getCount() {
		return listdata.size();
	}

	@Override
	public Object getItem(int arg0) {
		return listdata.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(final int arg0, View arg1, ViewGroup arg2) {
		Holder holder;
		if (arg0 != index && arg0 > index) {
			holder=new Holder();
			arg1=LayoutInflater.from(context).inflate(R.layout.img_fileadapter, null);
			holder.photo_imgview=(ImageView) arg1.findViewById(R.id.filephoto_imgview);
			holder.filecount_textview=(TextView) arg1.findViewById(R.id.filecount_textview);
			holder.filename_textView=(TextView) arg1.findViewById(R.id.filename_textview);
			arg1.setTag(holder);
			holderlist.add(arg1);
		}else{
			holder= (Holder)holderlist.get(arg0).getTag();
			arg1=holderlist.get(arg0);
		}
		
		holder.filename_textView.setText(listdata.get(arg0).get(filename));
		holder.filecount_textview.setText(listdata.get(arg0).get(filecount));
		
		String path=listdata.get(arg0).get(imgpath);
		
		 new BitmapHelp().displayImage(context, holder.photo_imgview, path);
		
//		if (bitmaps[arg0] == null) {
//			util.imgExcute(holder.photo_imgview,new ImgCallBack() {
//				@Override
//				public void resultImgCall(ImageView imageView, Bitmap bitmap,String path2) {
//					bitmaps[arg0]=bitmap;
////					String path=listdata.get(arg0).get(imgpath);
////					int degree =ImageTools.readPictureDegree(path);
////					  BitmapFactory.Options opts=new BitmapFactory.Options();//获取缩略图显示到屏幕上
////		                opts.inSampleSize=2;
////		                Bitmap cbitmap=BitmapFactory.decodeFile(path,opts);
////		                /** 
////		                 * 把图片旋转为正的方向 
////		                 */ 
////		                Bitmap newbitmap = ImageTools.rotaingImageView(degree, cbitmap);  
////		                imageView.setImageBitmap(newbitmap);
////					http://orgcent.com/android-outofmemoryerror-load-big-image/
//					
//					String path=listdata.get(arg0).get(imgpath);
////					int degree =ImageTools.readPictureDegree(path);
////					  BitmapFactory.Options opts=new BitmapFactory.Options();//获取缩略图显示到屏幕上
////		                opts.inSampleSize=2;
////		                opts.inJustDecodeBounds=true;
////		                BitmapFactory.decodeFile(listdata.get(arg0).get(imgpath), opts);
////		                // 计算图片缩放比例
////		                final int minSideLength = Math.min(50, 50);
////		                opts.inSampleSize = ImageTools.computeSampleSize(opts, minSideLength,
////		                        50 * 50);
////		                opts.inJustDecodeBounds = false;
////		                opts.inInputShareable = true;
////		                opts.inPurgeable = true;
////		                
////		            
////		                Bitmap cbitmap=BitmapFactory.decodeFile(path,opts);
////		                /** 
////		                 * 把图片旋转为正的方向 
////		                 */ 
////		                Bitmap newbitmap = ImageTools.rotaingImageView(degree, cbitmap);
////		       
////		                imageView.setImageBitmap(newbitmap);
//                     new BitmapHelp().displayImage(context, imageView, path);
//				}
//			}, listdata.get(arg0).get(imgpath));
//		}
//		else {
//			String path=listdata.get(arg0).get(imgpath);
////			int degree =ImageTools.readPictureDegree(path);
////			  BitmapFactory.Options opts=new BitmapFactory.Options();//获取缩略图显示到屏幕上
////                opts.inSampleSize=2;
////                opts.inJustDecodeBounds=true;
////                BitmapFactory.decodeFile(listdata.get(arg0).get(imgpath), opts);
////                // 计算图片缩放比例
////                final int minSideLength = Math.min(50, 50);
////                opts.inSampleSize = ImageTools.computeSampleSize(opts, minSideLength,
////                        50 * 50);
////                opts.inJustDecodeBounds = false;
////                opts.inInputShareable = true;
////                opts.inPurgeable = true;
////                Bitmap cbitmap=BitmapFactory.decodeFile(path,opts);
////                /** 
////                 * 把图片旋转为正的方向 
////                 */ 
////                Bitmap newbitmap = ImageTools.rotaingImageView(degree, cbitmap);
////               
////                holder.photo_imgview.setImageBitmap(newbitmap);
//
//               new BitmapHelp().displayImage(context, holder.photo_imgview, path);
//            
////			holder.photo_imgview.setImageBitmap(bitmaps[arg0]);
//		}
		
		return arg1;
	}
	
	class Holder{
		public ImageView photo_imgview;
		public TextView filecount_textview;
		public TextView filename_textView;
	}

	
	
}
